#include "file_config_item.h"

std::map<int, SBaseDataPiece> g_CfgPieceTable;
std::map<int, SBaseDataWeapon> g_CfgWeaponTable;
std::map<int, SBaseDataItem> g_CfgItemTable;
std::map<int, SBaseDataExhibitionElement> g_CfgExhibitionElementTable;
std::list<SBaseDataFormula> g_CfgFormulaTable;

void CfgItemLoadConfigFile(void)
{
	char _Buffer[256];
	FILE *_ConfigFile;
	mxml_node_t *_NodeXml, *_NodeBouy, *_NodeSubBouy;
	SBaseDataPiece _Piece;
	SBaseDataWeapon _Weapon;
	SBaseDataItem _Item;
	SBaseDataExhibitionElement _ExhibitionElement;
	SBaseDataFormula _Formula;

	memset(_Buffer, 0, sizeof _Buffer);
	[[CCFileUtils fullPathFromRelativePath:@"file_config_item.xml"] getCString:_Buffer maxLength:sizeof _Buffer encoding:NSASCIIStringEncoding];
	_ConfigFile = fopen(_Buffer, "r");
	if (NULL != _ConfigFile)
	{
		_NodeXml = mxmlLoadFile(NULL, _ConfigFile, MXML_TEXT_CALLBACK);
		_NodeBouy = mxmlGetFirstChild(_NodeXml);
		for (; NULL != _NodeBouy; _NodeBouy = mxmlWalkNext(_NodeBouy, _NodeXml, MXML_NO_DESCEND))
		{
			/*piece
			*/
			if (tmxmlIsElement(_NodeBouy, "piece"))
			{
				memset(&_Piece, 0, sizeof(SBaseDataPiece));
				_NodeSubBouy = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeSubBouy; _NodeSubBouy = mxmlWalkNext(_NodeSubBouy, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeSubBouy, "id"))
					{
						_Piece.id = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "path"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _Piece.path);
					}
				}
				g_CfgPieceTable.insert(std::pair<int, SBaseDataPiece>(_Piece.id, _Piece));
			}
			/*weapon
			*/
			else if (tmxmlIsElement(_NodeBouy, "weapon"))
			{
				_Weapon.description.clear();
				_NodeSubBouy = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeSubBouy; _NodeSubBouy = mxmlWalkNext(_NodeSubBouy, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeSubBouy, "id"))
					{
						_Weapon.id = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "name"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _Weapon.name);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "icon"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _Weapon.icon_path);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "description"))
					{
						tmxmlReadStringTable(_NodeSubBouy, &_Weapon.description, "data");
					}
				}
				g_CfgWeaponTable.insert(std::pair<int, SBaseDataWeapon>(_Weapon.id, _Weapon));
			}
			/*item
			*/
			else if (tmxmlIsElement(_NodeBouy, "item"))
			{
				_Item.description.clear();
				_NodeSubBouy = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeSubBouy; _NodeSubBouy = mxmlWalkNext(_NodeSubBouy, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeSubBouy, "id"))
					{
						_Item.id = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "name"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _Item.name);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "icon"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _Item.icon_path);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "description"))
					{
						tmxmlReadStringTable(_NodeSubBouy, &_Item.description, "data");
					}
				}
				g_CfgItemTable.insert(std::pair<int, SBaseDataItem>(_Item.id, _Item));
			}
			/*exhibition element
			*/
			else if (tmxmlIsElement(_NodeBouy, "element"))
			{
				_NodeSubBouy = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeSubBouy; _NodeSubBouy = mxmlWalkNext(_NodeSubBouy, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeSubBouy, "id"))
					{
						_ExhibitionElement.id = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "type"))
					{
						_ExhibitionElement.type = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "name"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _ExhibitionElement.name);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "path"))
					{
						tmxmlElementReadChildText(_NodeSubBouy, _ExhibitionElement.path);
					}
				}
				g_CfgExhibitionElementTable.insert(std::pair<int, SBaseDataExhibitionElement>(_ExhibitionElement.id, _ExhibitionElement));
			}
			/*formula
			*/
			else if (tmxmlIsElement(_NodeBouy, "formula"))
			{
				memset(&_Formula, 0, sizeof(SBaseDataFormula));
				_NodeSubBouy = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeSubBouy; _NodeSubBouy = mxmlWalkNext(_NodeSubBouy, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeSubBouy, "component_a"))
					{
						_Formula.component_a = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "component_b"))
					{
						_Formula.component_b = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "product_type"))
					{
						_Formula.product_type = tmxmlElementReadChildInt(_NodeSubBouy);
					}
					else if (tmxmlIsElement(_NodeSubBouy, "product_id"))
					{
						_Formula.product_id = tmxmlElementReadChildInt(_NodeSubBouy);
					}
				}
				g_CfgFormulaTable.push_back(_Formula);
			}
		}
		mxmlDelete(_NodeXml);
		fclose(_ConfigFile);

		if (g_FlagChinese)
		{
			/*weapon name
			*/
			strcpy(g_CfgWeaponTable[0].name, "桃心弓");
			strcpy(g_CfgWeaponTable[1].name, "闪电之弓");
			strcpy(g_CfgWeaponTable[2].name, "太阳之弓");
			strcpy(g_CfgWeaponTable[3].name, "恶魔之弓");
			strcpy(g_CfgWeaponTable[4].name, "天使之翼");

			/*weapon description
			*/
			g_CfgWeaponTable[0].description.clear();
			g_CfgWeaponTable[0].description.push_back(std::string("拥有桃心形状的弓,"));
			g_CfgWeaponTable[0].description.push_back(std::string("丘比特的最爱!"));

			g_CfgWeaponTable[1].description.clear();
			g_CfgWeaponTable[1].description.push_back(std::string("电光萦绕,"));
			g_CfgWeaponTable[1].description.push_back(std::string("拥有雷电的力量!"));

			g_CfgWeaponTable[2].description.clear();
			g_CfgWeaponTable[2].description.push_back(std::string("太阳的弓箭,"));
			g_CfgWeaponTable[2].description.push_back(std::string("缓慢但总是充满力量!"));

			g_CfgWeaponTable[3].description.clear();
			g_CfgWeaponTable[3].description.push_back(std::string("恶魔的弓,"));
			g_CfgWeaponTable[3].description.push_back(std::string("牺牲才有回报!"));

			g_CfgWeaponTable[4].description.clear();
			g_CfgWeaponTable[4].description.push_back(std::string("天使使用的弓,"));
			g_CfgWeaponTable[4].description.push_back(std::string("羽翼带给了他速度和能量!"));

			/*item
			*/
			strcpy(g_CfgItemTable[0].name, "防护罩");
			strcpy(g_CfgItemTable[1].name, "时间增加");
			strcpy(g_CfgItemTable[2].name, "时间静止");
			strcpy(g_CfgItemTable[3].name, "无敌");
			strcpy(g_CfgItemTable[4].name, "多重射击");
			strcpy(g_CfgItemTable[5].name, "爆炸箭");
			strcpy(g_CfgItemTable[6].name, "快速射击");

			g_CfgItemTable[0].description.clear();
			g_CfgItemTable[0].description.push_back(std::string("闪耀的防护罩"));
			g_CfgItemTable[0].description.push_back(std::string("可以为你阻挡一次攻击."));

			g_CfgItemTable[1].description.clear();
			g_CfgItemTable[1].description.push_back(std::string("时间就是金钱,"));
			g_CfgItemTable[1].description.push_back(std::string("这就是你所需要的."));

			g_CfgItemTable[2].description.clear();
			g_CfgItemTable[2].description.push_back(std::string("静止时间10秒钟,"));
			g_CfgItemTable[2].description.push_back(std::string("努力收集你的爱心吧"));

			g_CfgItemTable[3].description.clear();
			g_CfgItemTable[3].description.push_back(std::string("可以让你无敌10秒钟"));

			g_CfgItemTable[4].description.clear();
			g_CfgItemTable[4].description.push_back(std::string("多重箭,"));
			g_CfgItemTable[4].description.push_back(std::string("多重伤害,"));
			g_CfgItemTable[4].description.push_back(std::string("只有五发"));

			g_CfgItemTable[5].description.clear();
			g_CfgItemTable[5].description.push_back(std::string("带有爆炸物的箭,"));
			g_CfgItemTable[5].description.push_back(std::string("可以造成范围伤害."));

			g_CfgItemTable[6].description.clear();
			g_CfgItemTable[6].description.push_back(std::string("让你的弓没有冷却时间,"));
			g_CfgItemTable[6].description.push_back(std::string("尽情的射吧!"));

			/*exhibition
			*/
			strcpy(g_CfgExhibitionElementTable[0].name, "中文发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[1].name, "英文发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[2].name, "法语发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[3].name, "德语发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[4].name, "日语发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[5].name, "韩语发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[6].name, "俄语发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[7].name, "西班牙语发音的“我爱你”");
			strcpy(g_CfgExhibitionElementTable[8].name, "情诗一首");
			strcpy(g_CfgExhibitionElementTable[9].name, "情诗一首");
			strcpy(g_CfgExhibitionElementTable[10].name, "情诗一首");
			strcpy(g_CfgExhibitionElementTable[11].name, "情诗一首");
			strcpy(g_CfgExhibitionElementTable[12].name, "精美壁纸");
			strcpy(g_CfgExhibitionElementTable[13].name, "精美壁纸");
			strcpy(g_CfgExhibitionElementTable[14].name, "精美壁纸");
			strcpy(g_CfgExhibitionElementTable[15].name, "精美壁纸");
		}
	}
	else
	{
		exit(0);
	}
}